package com.imooc.controller;

import com.imooc.pojo.*;
import com.imooc.service.UserService;
import com.imooc.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;


@Controller
@Slf4j
@RequestMapping("/user")
public class UserContoller {
	@Autowired
	private UserService userService;

//	@RequestMapping("/updateUser")
//	@ResponseBody
//	public IMoocJSONResult getUserJson(String password, String id) {
//		val stringStringHashMap = new HashMap<String, String>();
//		stringStringHashMap.put("id", id);
//		stringStringHashMap.put("password", password);
//		userService.updateUser(stringStringHashMap);
//		return IMoocJSONResult.ok();
//	}

	@RequestMapping("jump/update/{userId}")
	public String jumpUser(ModelMap map, @PathVariable("userId") String userId) {
		map.addAttribute("userId", userId);
		return "thymeleaf/change_pwd";
	}

	@RequestMapping("login")
	public String loginAction(ModelMap map,String username, String password) {
//		map.addAttribute("resource", resource);
//		SysUser u = userService.queryUser(username, password);
//		if (null != u){jjjj
//			if (u.getUsername().equals("admin")){
//				List<SysUser> sysUsers = userService.queryUserInfo(new HashMap<String, String>());
//				map.addAttribute("sysUsers", sysUsers);
//				return "thymeleaf/admin_index";
//			} else {
//				List<Map<String, String>> list = sysDoorCountService.getCodeList();
//				map.addAttribute("codeList", list);
//				map.addAttribute("userId", u.getId());
				return "thymeleaf/index";
//			}
//		}
//		return "thymeleaf/login";
	}


	@RequestMapping("del/{userId}")
	@ResponseBody
	public IMoocJSONResult delUser(ModelMap map, @PathVariable("userId") String userId) {
		userService.deleteUser(userId);
		return IMoocJSONResult.ok();
	}

	@RequestMapping(value = "info", method = RequestMethod.GET)
	@ResponseBody
	public JsonResult userInfo(){
		List<SysUser> sysUsers = userService.queryUserInfo(new HashMap<String, String>());
		JsonResult result = JsonResult.ok(sysUsers);
//		log.info(JsonUtils.objectToJson(result));
		return  result;
	}
}
